package chain_of_responsibility;

public abstract class AbstractLogger {
    public static int INFO = 1;
    public static int DEBUG = 2;
    public static int ERROR = 3;
    protected int level;
    protected AbstractLogger nextLogger; // 责任链中的下一个元素

    // 设置责任链中的下一个元素
    public void setNextLogger(AbstractLogger nextLogger){
        this.nextLogger = nextLogger;
    }

    // handle
    abstract protected void write(String message);

    public void logMessage(int level, String message){
        // 判断 level 级别是否合法
        if(this.level <= level)
            write(message);
        // 继续调用责任链中的下一个元素
        if(nextLogger != null)
            nextLogger.logMessage(level, message);
    }
}